package cn.ht.jywrwater.module.ht.service.statisticsdevicecount;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.statisticsdevicecount.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.statisticsdevicecount.StatisticsDeviceCountDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.statisticsdevicecount.StatisticsDeviceCountConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.statisticsdevicecount.StatisticsDeviceCountMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 设备在线统计 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class StatisticsDeviceCountServiceImpl implements StatisticsDeviceCountService {

    @Resource
    private StatisticsDeviceCountMapper statisticsDeviceCountMapper;

    @Override
    public Long createStatisticsDeviceCount(StatisticsDeviceCountCreateReqVO createReqVO) {
        // 插入
        StatisticsDeviceCountDO statisticsDeviceCount = StatisticsDeviceCountConvert.INSTANCE.convert(createReqVO);
        statisticsDeviceCountMapper.insert(statisticsDeviceCount);
        // 返回
        return statisticsDeviceCount.getId();
    }

    @Override
    public void updateStatisticsDeviceCount(StatisticsDeviceCountUpdateReqVO updateReqVO) {
        // 校验存在
        validateStatisticsDeviceCountExists(updateReqVO.getId());
        // 更新
        StatisticsDeviceCountDO updateObj = StatisticsDeviceCountConvert.INSTANCE.convert(updateReqVO);
        statisticsDeviceCountMapper.updateById(updateObj);
    }

    @Override
    public void deleteStatisticsDeviceCount(Long id) {
        // 校验存在
        validateStatisticsDeviceCountExists(id);
        // 删除
        statisticsDeviceCountMapper.deleteById(id);
    }

    private void validateStatisticsDeviceCountExists(Long id) {
        if (statisticsDeviceCountMapper.selectById(id) == null) {
            throw exception(STATISTICS_DEVICE_COUNT_NOT_EXISTS);
        }
    }

    @Override
    public StatisticsDeviceCountDO getStatisticsDeviceCount(Long id) {
        return statisticsDeviceCountMapper.selectById(id);
    }

    @Override
    public List<StatisticsDeviceCountDO> getStatisticsDeviceCountList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return statisticsDeviceCountMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StatisticsDeviceCountDO> getStatisticsDeviceCountPage(StatisticsDeviceCountPageReqVO pageReqVO) {
        return statisticsDeviceCountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StatisticsDeviceCountDO> getStatisticsDeviceCountList(StatisticsDeviceCountExportReqVO exportReqVO) {
        return statisticsDeviceCountMapper.selectList(exportReqVO);
    }

}
